package com.chanyachting.emall.action.exhibition;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Productcategory;
import com.chanyachting.emall.domain.Showyacht;
import com.chanyachting.emall.form.ExhibitionForm;
import com.chanyachting.emall.service.ChannelService;
import com.chanyachting.emall.service.CompanyService;
import com.chanyachting.emall.service.ProductcategoryService;
import com.chanyachting.emall.service.ShowyachtService;
import com.chanyachting.emall.serviceImpl.ChannelServiceImpl;
import com.chanyachting.emall.serviceImpl.CompanyServiceImpl;
import com.chanyachting.emall.serviceImpl.ProductcategoryServiceImpl;
import com.chanyachting.emall.serviceImpl.ShowyachtServiceImpl;

public class QueryOneExhibitionsAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ExhibitionForm exForm = (ExhibitionForm) form;
		exForm.setCurrentPage(1);
		ShowyachtService syService=new ShowyachtServiceImpl();
		CompanyService cpService = new CompanyServiceImpl();
		ChannelService cService = new ChannelServiceImpl();
		ProductcategoryService pcService = new ProductcategoryServiceImpl();
		
		String exhibitionId = exForm.getExhibitionId();
		
		if (exhibitionId == null || "".equals(exhibitionId)) {
			return mapping.findForward("error");
		}
		
		Showyacht s = syService.getOneShowyachtById(exhibitionId);
		if (s == null) {
			return mapping.findForward("error");
		}
		
		int companyId = s.getCompanyId();
		Company comp;
		try {
			comp = cpService.getOneCompany(new Company(companyId));
		} catch (Exception e) {
			return mapping.findForward("error");
		}
		
		
		int channelId = exForm.getChannelId();
		Channel c = cService.getOneChannel(new Channel(channelId));
		if (c == null) {
			return mapping.findForward("error");
		}
		
		List<Channel> subChannelList = cService.getSubChannel(c);
		
		if (subChannelList == null || subChannelList.size() == 0) {
			return mapping.findForward("error");
		}
		Productcategory pc = pcService.getOneProductcategoryById(String.valueOf(s.getProductCategoryId()));
		if (pc == null) {
			return mapping.findForward("error");
		}
		exForm.setExhibitionName(pc.getName());
		exForm.setSubChannelList(subChannelList);
		exForm.setSy(s);
		exForm.setComp(comp);
		
		return mapping.findForward("readOneExhibition");
	}
}
